// pages/TradingArea/couponDetail.js
const controller=require("../controllers/Controller").controller

const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    couponId:"",
    detail:"",
    couponBagId:'',
    show:false
  },

  //获取弹窗消息
  getMsg(){
    let that=this
    controller.getNotice().then(res=>{
      this.setData({
        msg:res.data,
        show:true
      })

      setTimeout(function(){
        that.setData({
          show:false
        })
      },6500)
    })
  },


  //获取优惠券详情
  getCouponDetail(){
    controller.shareCouponDetail({couponId:this.data.couponId}).then(res=>{
      this.setData({
        detail:res.data
      })
    })
  },

  toShopDetail(e){
    let shopId=e.currentTarget.dataset.shopid
    wx.navigateTo({
      url: '/pages/Index/Index?shopId='+shopId,
    })
  },
  phone(e){
    let phone=e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },

  toGoodsDetail(e){
    let shopId=e.currentTarget.dataset.shopid
    let id=e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/myCenter/shopGoodsDetail?shopId='+shopId+"&id="+id,
    })
  },


  //生成订单购买
  createCouponOrder(){
    let value={}
    value.couponBagId=this.data.couponBagId
    if(this.data.invitationAccountId){
      value.invitationAccountId=this.data.invitationAccountId
    }
    controller.createCouponOrder(value).then(res=>{
      let data=res.data
      this.pay(data)
    })
  },

  //分享海报
  sharePoster(){
    controller.getSharePoster({couponBagId:this.data.couponBagId}).then(res=>{
      this.setData({
        shareImg:res.data
      })
      let list=[]
      list.push(res.data)
      wx.previewImage({
        urls: list,
      })
    })
  },

  pay(data){
    let that=this
    let value={}
    value.openId=app.globalData.openId
    value.orderNo=data.orderNo
    value.type="GC"
    controller.weChatPay(value).then(res=>{
      let data=JSON.parse(res.data)
      wx.requestPayment({
        nonceStr: data.nonceStr,
        package: data.package,
        paySign: data.paySign,
        timeStamp: data.timeStamp,
        signType: data.signType,
        success(res){
          wx.showToast({
            title: "支付成功",
            icon: 'none',
            duration: 1000
          });
          setTimeout(function(){
            wx.showActionSheet({
              itemList: ['去查看', '继续浏览'],
              success (res) {
                let index=res.tapIndex
                console.log(res)
                if(index==0){
                  if(that.data.type===0){
                    wx.navigateTo({
                      url: '/pages/myCenter/shopCoupon',
                    })
                  }else{
                    wx.navigateTo({
                      url: '/pages/myCenter/tradingArea',
                    })
                  }
                }else{
                  wx.reLaunch({
                    url: '/pages/TradingArea/TradingArea',
                  })
                }
              },
              fail (res) {
                console.log(res.errMsg)
              }
            })
          },1500)
        },
        fail(res){
          wx.showToast({
            title: "支付失败",
            icon: 'none',
            duration: 1000
          });
        }
      })
    })
  },

    //礼券包详情 2.0
  getBagActivityDetail(){
    let that=this
    controller.getActivityDetail({couponBagId:this.data.couponBagId}).then(res=>{
      let data=res.data
      data.startTime=res.data.startTime.split(" ")[0]
      data.endTime=res.data.endTime.split(" ")[0]
      this.setData({
        bagDetail:data,
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      couponId:options.id,
      couponBagId:options.couponBagId
    })

    if(options.invitationAccountId){
      this.setData({
        invitationAccountId:options.invitationAccountId
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCouponDetail()
    this.getBagActivityDetail()

    let that=this
    this.setData({
      timer:setInterval(function(){
        let page=getCurrentPages()
        if(page[page.length-1].route!='pages/TradingArea/couponDetail'){
          return
        }
        console.log("1")
        that.getMsg()
      },10000)
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let that=this
    clearInterval(that.data.timer)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})